﻿package com.google.code.p.ivans.patternsentities;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Scanner;

import org.apache.log4j.Logger;

import com.google.code.p.ivans.applications.Application_6_XML;
import com.google.code.p.ivans.entities.Order;
import com.google.code.p.ivans.entities.Waiter;
import com.google.code.p.ivans.serviceinterfaces.OrderServiceInterface;
import com.google.code.p.ivans.serviceinterfaces.WaitersOrdersServiceInterface;
import com.google.code.p.ivans.serviceinterfacesimpls.OrderServiceInterfaceImpl;
import com.google.code.p.ivans.serviceinterfacesimpls.WaiterServiceInterfaceImpl;
import com.google.code.p.ivans.serviceinterfacesimpls.WaitersOrdersServiceInterfaceImpl;

public class Main {
	
	public static final Logger logger = Logger.getLogger(Main.class);
	
	public static WaiterServiceInterfaceImpl waiterSI;
	public static OrderServiceInterface orderSI;
	public static WaitersOrdersServiceInterface waitersOrdersSI;
	
	public static Scanner scanner;
	public static BufferedReader bufread;
	
	private static Application application;
	
	static {
		waiterSI = new WaiterServiceInterfaceImpl();
		orderSI = new OrderServiceInterfaceImpl();
		waitersOrdersSI = new WaitersOrdersServiceInterfaceImpl();
		
		scanner = new Scanner(System.in);
		bufread = new BufferedReader(new InputStreamReader(System.in));	
		
	}
	
	public static void main(String[] args) {		
		
		application = new Application();
		
		AbstractMenuFactory menuFactory= new MenuFactory();
		
		// MenuItems's
		
		MenuItem addWaiterItem = menuFactory.createMenuItem("добавить официанта", 
															new AddWaiterHandler());
		MenuItem addOrderItem = menuFactory.createMenuItem("добавить заказ", 
															new AddOrderHandler());
		MenuItem addWaitersOrdersItem = menuFactory.createMenuItem("добавить связь официант<->заказ",
																	new AddWaitersOrdersHandler());		
		MenuItem changeWaiterFirstNameItem = menuFactory.createMenuItem("изменить имя", 
																	new ChangeWaiterNameHandler());
		MenuItem changeWaiterLastNameItem = menuFactory.createMenuItem("изменить фамилию", 
																new ChangeWaiterLastNameHandler());
		MenuItem changeWaiterPatronymicNameItem = menuFactory.createMenuItem("изменить отчество", 
																new ChangeWaiterPatronymicNameHandler());		
		MenuItem changeWaiterSalaryItem = menuFactory.createMenuItem("изменить зарплату", 
																new ChangeWaiterSalaryHandler());
		MenuItem deleteWaiterItem = menuFactory.createMenuItem("удалить сотрудника", 
																new DeleteWaiterHandler());
		MenuItem openWaiterByIdItem = menuFactory.createMenuItem("открыть официанта по id", 
																new OpenWaiterByIdHandler());
		MenuItem openWaiterByNameItem = menuFactory.createMenuItem("поиск официанта по имени", 
																new OpenWaiterByNameHandler());
		MenuItem openOrderItem = menuFactory.createMenuItem("открыть заказ по id", 
																new OpenOrderHandler());
		MenuItem showAllWaitersItem = menuFactory.createMenuItem("открыть всех официантов", 
																new ShowAllWaitersHandler());
		MenuItem showOrdersItem = menuFactory.createMenuItem("открыть все заказы", 
																new ShowOrdersHandler());
		MenuItem showOrdersByWaiterItem = menuFactory.createMenuItem("открыть все заказы официанта", 
																new ShowAllWaitersHandler());
		MenuItem showWaitersByOrderItem = menuFactory.createMenuItem("открыть официантов по заказу", 
																new ShowWaitersByOrderHandler());
		
		// главное меню
		
		SubMenu mainSubMenu = menuFactory.createSubMenu("главное меню");
		
		SubMenu waitersSubMenu = menuFactory.createSubMenu("официанты");
		
		SubMenu ordersSubMenu = menuFactory.createSubMenu("заказы");
		
		SubMenu waitersOrdersSubMenu = menuFactory.createSubMenu("заказы_официанты");
		
		mainSubMenu.addMenu(waitersSubMenu);
		mainSubMenu.addMenu(ordersSubMenu);
		mainSubMenu.addMenu(waitersOrdersSubMenu);
		
		// официанты
		
		SubMenu openWaiterSubMenu = menuFactory.createSubMenu("открыть");
		
		SubMenu changeWaiterSubMenu = menuFactory.createSubMenu("изменить данные официанта");
		
		waitersSubMenu.addMenu(addWaiterItem);
		waitersSubMenu.addMenu(openWaiterSubMenu);
		waitersSubMenu.addMenu(showAllWaitersItem);
		waitersSubMenu.addMenu(changeWaiterSubMenu);
		waitersSubMenu.addMenu(deleteWaiterItem);
		waitersSubMenu.addMenu(showOrdersByWaiterItem);
		
		// заказы
		
		ordersSubMenu.addMenu(addOrderItem);
		ordersSubMenu.addMenu(openOrderItem);
		ordersSubMenu.addMenu(showOrdersItem);
		ordersSubMenu.addMenu(showWaitersByOrderItem);
		
		// подменю открыть официанта
		
		openWaiterSubMenu.addMenu(openWaiterByIdItem);
		openWaiterSubMenu.addMenu(openWaiterByNameItem);
		
		// подменю изменить официанта
		
		changeWaiterSubMenu.addMenu(changeWaiterFirstNameItem);
		changeWaiterSubMenu.addMenu(changeWaiterLastNameItem);
		changeWaiterSubMenu.addMenu(changeWaiterPatronymicNameItem);
		changeWaiterSubMenu.addMenu(changeWaiterSalaryItem);
		
		// подменю официанты_заказы
		
		waitersOrdersSubMenu.addMenu(addWaitersOrdersItem);	
		
		application.setSubMenu(mainSubMenu);
		
		while(true) {
			application.run();
		}
		
	}
	
	public static Application getApplication() {
		return application;
	}
	
	public static String showWaiters(List<Waiter> waiters) {
		if(waiters == null)
			return null;
		
		if(waiters.size() == 0)
			return null;
		
		StringBuilder all = new StringBuilder();
		String label = "ID   ФИО                                         Зарплата Дата рождения Дата принятия ";
		all.append(label);
		all.append("\n");
		
		//System.out.println(label);
				
		for(int i = 0; i < waiters.size(); i++) {
			long id = waiters.get(i).getWaiterID();
			int signs = 0;
			while(id != 0) {
				id /= 10;
				signs++;
			}
			
			StringBuilder builder = new StringBuilder();
 			builder.append(waiters.get(i).getWaiterID());
 			
 			for(int j = signs; j < 5; j++) {
 				builder.append(' ');
 			}
 			
 			String FIO = waiters.get(i).getLastName() + " " + waiters.get(i).getFirstName() + " " + waiters.get(i).getPatronymicName();
 			builder.append(FIO);
 	
 			for(int j = FIO.length(); j < 44; j++) {
 				builder.append(' ');
 			}
 			
 			int salary = waiters.get(i).getSalary();
 			signs = 0;
 			while(salary != 0) {
				salary /= 10;
				signs++;
			}
 			
 			builder.append(waiters.get(i).getSalary());
 			for(int j = signs; j < 9; j++) {
 				builder.append(' ');
 			}
 			
 			GregorianCalendar bornDate = waiters.get(i).getBornDate();
 			
 			int day = bornDate.get(Calendar.DAY_OF_MONTH);
 			int month = (bornDate.get(Calendar.MONTH) + 1); 			
 			String born = ((day < 10) ? "0" + day:day)   + "-" + ((month < 10) ? "0" + month:month) + "-" + bornDate.get(Calendar.YEAR) + "    ";
 			
 			builder.append(born);  
 			
 			GregorianCalendar hireDate = waiters.get(i).getHireDate();
 			
 			day = hireDate.get(Calendar.DAY_OF_MONTH);
 			month = (hireDate.get(Calendar.MONTH) + 1); 
 			String hire = ((day < 10) ? "0" + day:day)   + "-" + ((month < 10) ? "0" + month:month) + "-" + hireDate.get(Calendar.YEAR) + "    ";
 			
 			builder.append(hire); 
 			//System.out.println(builder.toString());
 			all.append(builder);
 			all.append("\n");
		}
		return all.toString();
	}	
	
	public static String showOrders(List<Order> orders) {
		//println("------------------------------------");
		StringBuilder bld = new StringBuilder();
		bld.append("------------------------------------\n");
		for(int i = 0; i < orders.size(); i++) {
			//System.out.println();
			//System.out.println(orders.get(i));
			//System.out.println();
			bld.append(orders.get(i).toString());
			bld.append("\n");
		}
		//println("------------------------------------");
		bld.append("------------------------------------\n");
		return bld.toString();
	}

}
